var session = sessionStorage.getItem("session");
if (!session) {
	window.parent.location = "../../login.html";
}
var cityIndex;
$(document).ready(function() {
	var json1 = [
		['1', '硬探针'],
		['2', '软探针'],
		['3', '其他']
	];
	$("#type").combobox({
		data: json1,
		valueField: 0,
		textField: 1,
		editable: false,
	});

	var json2 = [
		['CDMA'],
		['GSM'],
		['WCDMA']
	];
	$("#netType").combobox({
		data: json2,
		valueField: 0,
		textField: 0,
		editable: false,
	});

	$("#city").combobox({
		url: IP + "GetAreaInfoList_Combobox",
		type: 'Get',
		dataType: "jsonp",
		crossDomain: true,
		valueField: 'text',
		editable: false,
		textField: 'text',
		onLoadSuccess: function() { 
			var val = $(this).combobox("getData");
			for (var item in val[0]) {
				if (item == "text") {
					$(this).combobox("select", val[0][item]);
					cityIndex = val[0][item];
				}
			}

		}
	});

	$("#jqGrid").jqGrid({
		url: IP + "GetProbeInfoList?callback=?&session=" + session,
		mtype: "Get",
		datatype: "jsonp",
		colModel: [{
			label: '序号',
			sorttype: 'integer',
			name: 'id',
			key: true,
			width: 40,
			hidden: true,
		}, {
			label: '设备名称',
			name: 'deviceName',
			width: 80
		}, {
			label: '设备编码',
			name: 'deviceid',
			width: 80
		}, {
			label: '县市',
			name: 'city',
			width: 80
		}, {
			label: '客户名称',
			name: 'customerName',
			width: 80
		}, {
			label: '客户电话',
			name: 'customerTel',
			width: 100
		}, {
			label: '客户经理',
			name: 'customerManager',
			width: 80
		}, {
			label: '客户经理电话',
			name: 'customerManagerTel',
			width: 100
		}, {
			label: '操作者',
			name: 'operator',
			width: 80
		}, {
			label: '网络类型',
			name: 'netType',
			width: 80
		}, {
			label: 'IP',
			name: 'ip',
			width: 80
		}, {
			label: '安装地址',
			name: 'install',
			width: 100
		}, {
			label: '设备类型',
			name: 'type',

			width: 80
		}, {
			label: '设备状态',
			name: 'deviceStatus',
			width: 80,
			formatter: "select",
			editoptions: {value:"0:离线;1:在线;"},
		}, {
			label: 'AC',
			name: 'ac',
			width: 80,
			hidden: true,
		}, {
			label: 'wifi名称',
			name: 'wifiName',
			width: 80,
			hidden: true,

		}, {
			label: 'wifi密码',
			name: 'wifiPwd',
			width: 80,
			hidden: true,

		}, {
			label: '端口',
			name: 'port',
			width: 80,

		}, {
			label: '小区',
			name: 'community',
			width: 80,

		}, {
			label: 'ONU',
			name: 'onu',
			width: 80,
			hidden: true,
		}, {
			label: 'OLT',
			name: 'olt',
			width: 80,
			hidden: true,
		}, {
			label: 'BIAS',
			name: 'bias',
			width: 80,
			hidden: true,
		}, {
			label: '最后一次心跳',
			name: 'lastHeartbeat',
			width: 100,

		}, {
			label: '经度',
			name: 'longitude',
			width: 80,
			hidden: true,
		}, {
			label: '纬度',
			name: 'latitude',
			width: 80,
			hidden: true,
		}, {
			label: '新增时间',
			name: 'newTime',
			width: 80,

		}, {
			label: '更新时间',
			name: 'updateTime',
			width: 80,

		}, {
			label: '备注',
			name: 'memo',
			width: 150
		}],
		loadonce: true,
		viewrecords: true,
		autowidth: true,
		rowNum: 10,
		rownumbers: true,
		page: 1,
		loadtext: '正在加载数据',
		pager: "#jqGridPager",
		loadComplete: function(data) {
			$('#cc').combo({
				editable: false,
				multiple: true,
				panelHeight: 320,
				onHidePanel: function() {
					var arr = [ 'city', 'customerName', 'customerTel', 'customerManager', 'customerManagerTel',
						'operator', 'netType', 'ip', 'install', 'deviceName', 'deviceid', 'type', 'deviceStatus', 'ac', 'wifiName',"community", 
						'wifiPwd', 'port', 'onu', 'olt', 'bias', 'lastHeartbeat', 'longitude', 'latitude', 'newTime', 'updateTime', 'meno'
					]
					for (var n = 0; n < arr.length; n++) {
						jQuery("#jqGrid").setGridParam().showCol(arr[n]).trigger("reloadGrid");
					}
					var chk_value = [];
					$('#sp  input:checked').each(function() {
						chk_value.push($(this).val());
					});
					for (var i = 0; i < chk_value.length; i++) {
						jQuery("#jqGrid").setGridParam().hideCol(chk_value[i]).trigger("reloadGrid");
					}

					var w = $("#deviceEdit").width();
					$("#jqGrid").jqGrid("setGridWidth", w)
				}
			});
			$('#sp').appendTo($('#cc').combo('panel'));

		}

	});
	var h = $("body").height();
	$("#jqGrid").jqGrid("setGridHeight",h-340)

	var template1 = {
		"groupOp": "AND",
		"rules": [{
			"field": "deviceName",
			"op": "eq",
			"data": "W"
		}, {
			"field": "port",
			"op": "le",
			"data": "15"
		}]
	};
	// activate the build in search with multiple option
	$('#jqGrid').navGrid("#jqGridPager", {
			search: true, // show search button on the toolbar
			add: false,
			edit: false,
			del: false,
			refresh: true
		}, {}, // edit options
		{}, // add options
		{}, // delete options
		{
			multipleSearch: true,
			multipleGroup: true,
			showQuery: true,
			// set the names of the template
			tmplNames: ["模板一"],
			// set the template contents
			tmplFilters: [template1]
		} // search options - define multiple search
	);
	AddRow();
});

function AddRow() {
	$('#deviceEdit').form('load', {
		id: 0,
		deviceName: '',
		deviceid: '',
		ip: '',

		ac: '',
		customerName: '',
		customerTel: '',
		customerManager: '',
		customerManagerTel: '',
		wifiName: '',
		wifiPwd: '',
		operator: sessionStorage.getItem("realName"),
		community: '',
		onu: '',
		olt: '',
		bias: '',
		prefecture: '',

		longitude: '',
		latitude: '',
		deviceStatus: '',
		lastHeartbeat: '',
		newTime: '',
		updateTime: '',

		install: '',
		port: 8080,
		memo: '',
	});
	$("#type").combobox("setValue", "1");
	$("#netType").combobox("setValue", "GSM");
	$("#city").combobox("setValue", cityIndex);
	$('#jqGrid').jqGrid('setSelection', id);
}

function EditRow() {
	var rowKey = $("#jqGrid").jqGrid('getGridParam', "selrow");
	if (rowKey) {
		var type = $("#jqGrid").getCell(rowKey, "type");
		$("#type").combobox("setValue", type);
		var netType = $("#jqGrid").getCell(rowKey, "netType");
		$("#netType").combobox("setValue", netType);
		var city = $("#jqGrid").getCell(rowKey, "city");
		console.log(city)
		$("#city").combobox("setValue", city);
		$('#deviceEdit').form('load', {
			id: rowKey,
			deviceName: $("#jqGrid").getCell(rowKey, "deviceName"),
			deviceid: $("#jqGrid").getCell(rowKey, "deviceid"),
			ip: $("#jqGrid").getCell(rowKey, "ip"),

			ac: $("#jqGrid").getCell(rowKey, "ac"),
			customerName: $("#jqGrid").getCell(rowKey, "customerName"),
			customerTel: $("#jqGrid").getCell(rowKey, "customerTel"),
			customerManager: $("#jqGrid").getCell(rowKey, "customerManager"),
			customerManagerTel: $("#jqGrid").getCell(rowKey, "customerManagerTel"),
			wifiName: $("#jqGrid").getCell(rowKey, "wifiName"),
			wifiPwd: $("#jqGrid").getCell(rowKey, "wifiPwd"),
			operator: $("#jqGrid").getCell(rowKey, "operator"),
			community: $("#jqGrid").getCell(rowKey, "community"),
			onu: $("#jqGrid").getCell(rowKey, "onu"),
			olt: $("#jqGrid").getCell(rowKey, "olt"),
			bias: $("#jqGrid").getCell(rowKey, "bias"),
			prefecture: $("#jqGrid").getCell(rowKey, "prefecture"),

			longitude: $("#jqGrid").getCell(rowKey, "longitude"),
			latitude: $("#jqGrid").getCell(rowKey, "latitude"),
			deviceStatus: $("#jqGrid").getCell(rowKey, "deviceStatus") == "1" ? "在线" : "离线",
			lastHeartbeat: $("#jqGrid").getCell(rowKey, "lastHeartbeat"),
			newTime: $("#jqGrid").getCell(rowKey, "newTime"),
			updateTime: $("#jqGrid").getCell(rowKey, "updateTime"),

			install: $("#jqGrid").getCell(rowKey, "install"),
			port: $("#jqGrid").getCell(rowKey, "port"),
			memo: $("#jqGrid").getCell(rowKey, "memo"),
		});
	} else {
		$.messager.alert('提醒', "没有选中行", 'info');
	}
}

function DeleteRow() {
	var grid = $("#jqGrid");
	var rowKey = grid.jqGrid('getGridParam', "selrow");
	var oldvalue = $("#jqGrid").getCell(rowKey, "deviceName");
	if (rowKey) {
		$.messager.confirm('删除提醒', '你确认要删除探针---' + $("#jqGrid").getCell(rowKey, "deviceName") + '?', function(r) {
			if (r) {

				$.ajax({
					type: "post", // 与可以 "get"
					url: IP + "DeleteProbeInfo?callback=?",
					data: {
						session: session,
						id: rowKey,
					},
					dataType: "jsonp",
					jsonp: "callback",
					jsonpCallback: "resultHandler",
					success: function(json) {
						if (json.result == 200) {
							$("#jqGrid").delRowData(rowKey);
							insertopertlog('探针管理', '探针管理', '删除', oldvalue, '');
							$.messager.show({
								title: '提醒',
								msg: '删除成功',
								timeout: 1300,
								showType: 'slide',
								style: {
									right: '',
									bottom: ''
								}
							});
						} else {
							$.messager.alert('删除失败', json.message, 'error');
						}
					}
				});
			}
		});
	} else {
		$.messager.alert('提醒', "没有选中行", 'info');
	}
}

function SaveRow() {
	var grid = $("#jqGrid");
	var rowKey = grid.jqGrid('getGridParam', "selrow");
	var oldvalue = $("#jqGrid").getCell(rowKey, "deviceName");
	var type = $("#type").combobox("getText");
	var netType = $("#netType").combobox("getText");
	var city = $("#city").combobox("getText");

	console.log(city);
	if ($('#deviceEdit').form('enableValidation').form('validate')) {

		$.ajax({
			type: "post", // 与可以 "get"
			url: IP + "UpdateProbeinfo?callback=?",
			data: {
				session: session,
				id: $("#id").val(),
				deviceName: $("#deviceName").val(),
				deviceid: $("#deviceid").val(),
				ip: $("#ip").val(),
				type: type,
				ac: $("#ac").val(),
				customerName: $("#customerName").val(),
				customerTel: $("#customerTel").val(),
				customerManager: $("#customerManager").val(),
				customerManagerTel: $("#customerManagerTel").val(),
				wifiName: $("#wifiName").val(),
				wifiPwd: $("#wifiPwd").val(),
				operator: $("#operator").val(),
				community: $("#community").val(),
				onu: $("#onu").val(),
				olt: $("#olt").val(),
				bias: $("#bias").val(),
				prefecture: $("#prefecture").val(),
				city: city,
				longitude: $("#longitude").val(),
				latitude: $("#latitude").val(),
				deviceStatus: $("#deviceStatus").val(),
				newTime: new Date(),
				updateTime: $("#updateTime").val(),
				netType: netType,
				install: $("#install").val(),
				port: $("#port").val(),
				memo: $("#memo").val(),
			},
			dataType: "jsonp",
			jsonp: "callback",
			jsonpCallback: "resultHandler",
			success: function(json) {
				if (json.result == 200) {
					var aaa = $("#id").val();
					if ($('#id').val() == 0) {
						$("#jqGrid").addRowData(json.id, {
							id: json.id,
							deviceName: $("#deviceName").val(),
							deviceid: $("#deviceid").val(),
							ip: $("#ip").val(),
							type: type,
							ac: $("#ac").val(),
							customerName: $("#customerName").val(),
							customerTel: $("#customerTel").val(),
							customerManager: $("#customerManager").val(),
							customerManagerTel: $("#customerManagerTel").val(),
							wifiName: $("#wifiName").val(),
							wifiPwd: $("#wifiPwd").val(),
							operator: $("#operator").val(),
							community: $("#community").val(),
							onu: $("#onu").val(),
							olt: $("#olt").val(),
							bias: $("#bias").val(),
							prefecture: $("#prefecture").val(),
							city: city,
							longitude: $("#longitude").val(),
							latitude: $("#latitude").val(),
							deviceStatus: $("#deviceStatus").val(),
							lastHeartbeat: $("#lastHeartbeat").val(),
							newTime: $("#newTime").val(),
							updateTime: $("#updateTime").val(),
							netType: netType,
							install: $("#install").val(),
							port: $("#port").val(),
							memo: $("#memo").val(),
						}, '');
						insertopertlog('探针管理', '探针管理', '新增', '', $("#deviceName").val());
						$.messager.show({
							title: '提醒',
							msg: '参数增加成功',
							timeout: 1300,
							showType: 'slide',
							style: {
								right: '',
								bottom: ''
							}
						});
					} else {
						$("#jqGrid").setRowData($('#id').val(), {
							id: $("#id").val(),
							deviceName: $("#deviceName").val(),
							deviceid: $("#deviceid").val(),
							ip: $("#ip").val(),
							type: type,
							ac: $("#ac").val(),
							customerName: $("#customerName").val(),
							customerTel: $("#customerTel").val(),
							customerManager: $("#customerManager").val(),
							customerManagerTel: $("#customerManagerTel").val(),
							wifiName: $("#wifiName").val(),
							wifiPwd: $("#wifiPwd").val(),
							operator: $("#operator").val(),
							community: $("#community").val(),
							onu: $("#onu").val(),
							olt: $("#olt").val(),
							bias: $("#bias").val(),
							prefecture: $("#prefecture").val(),
							city: city,
							longitude: $("#longitude").val(),
							latitude: $("#latitude").val(),
							deviceStatus: $("#deviceStatus").val(),
							lastHeartbeat: $("#lastHeartbeat").val(),
							newTime: $("#newTime").val(),
							updateTime: $("#updateTime").val(),
							netType: netType,
							install: $("#install").val(),
							port: $("#port").val(),
							memo: $("#memo").val(),
						}, '');
						insertopertlog('探针管理', '探针管理', '修改', oldvalue, $("#deviceName").val());
						$.messager.show({
							title: '提醒',
							msg: '探针参数修改成功',
							timeout: 1300,
							showType: 'slide',
							style: {
								right: '',
								bottom: ''
							}
						});
					}
				} else {
					$.messager.alert('保存失败', json.message, 'error');
				}
				//保存成功后，重置控件，以免用户重复提交
				AddRow();
			}
		});
	}
}